package com.baizhiedu.rsm.controller;

import com.baizhiedu.rsm.dto.ShowAnswer;
import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.service.AnswerService;
import com.baizhiedu.rsm.util.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lby
 */
@Controller
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;

    /**
     * 提交学生的回答至答案表 需要包含日期 问题id 学生id（Session）
     * @return
     */
    @RequestMapping("submitAnswer")
    @ResponseBody
    public String submitAnswer(Integer subjectId, HttpSession session,String answerText){
        StudentMessage user = (StudentMessage) session.getAttribute("user");
        System.out.println(user);
        System.out.println(subjectId);
        System.out.println(answerText);
        return answerService.submitAnswer(subjectId,answerText,user.getId());
    }

    /**
     * 获取需要展示的答案数据
     * 根据时间
     * @return
     */
    @RequestMapping("showAnswers")
    @ResponseBody
    public List<ShowAnswer> showAnswers(Date answerDate){
        if(answerDate == null){
            answerDate = new DateConverter().convert(DateFormat.getDateInstance().format(new Date()));
        }
        return answerService.showAnswers(answerDate);
    }
}
